package io.flutter.facade;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.taobao.idlefish.flutterboost.containers.BoostFlutterActivity;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;

import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugins.GeneratedPluginRegistrant;

public abstract class DKBaseFlutterPageActivity extends BoostFlutterActivity {
    private DkFlutterInterface proxy;

    public void setProxy(DkFlutterInterface proxy) {
        this.proxy = proxy;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusBar();
        if (null != proxy) {
            proxy.onCreate(savedInstanceState);
        }
    }

    @Override
    public void onRegisterPlugins(PluginRegistry registry) {
        GeneratedPluginRegistrant.registerWith(registry);
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (null != proxy) {
            proxy.onNewIntent(intent);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (null != proxy) {
            proxy.onStart();
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (null != proxy) {
            proxy.onRestart();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (null != proxy) {
            proxy.onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (null != proxy) {
            proxy.onPause();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (null != proxy) {
            proxy.onStop();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != proxy) {
            proxy.onDestroy();
        }
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        if (null != proxy) {
            proxy.onLowMemory();
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (null != proxy) {
            proxy.onSaveInstanceState(outState);
        }
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if (null != proxy) {
            proxy.onRestoreInstanceState(savedInstanceState);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (null != proxy) {
            proxy.onActivityResult(requestCode, resultCode, data);
        }
    }

    /**
     * 该方法返回当前Activity在Flutter层对应的name，
     * 混合栈将会在flutter层根据这个名字，在注册的Route表中查找对应的Widget
     * <p>
     * 在flutter层有注册函数：
     * FlutterBoost.singleton.registerPageBuilders({
     * 'first': (pageName, params, _) => FirstRouteWidget(),
     * 'second': (pageName, params, _) => SecondRouteWidget(),
     * ...
     * });
     * <p>
     * 该方法中返回的就是注册的key：first , second
     *
     * @return
     */
    @Override
    public abstract String getContainerName();

    /**
     * 该方法返回的参数将会传递给上层的flutter对应的Widget
     * <p>
     * 在flutter层有注册函数：
     * FlutterBoost.singleton.registerPageBuilders({
     * 'first': (pageName, params, _) => FirstRouteWidget(),
     * 'second': (pageName, params, _) => SecondRouteWidget(),
     * ...
     * });
     * <p>
     * 该方法返回的参数就会封装成上面的params
     *
     * @return
     */
    @Override
    public abstract Map getContainerParams();

    private void setStatusBar() {
        setDarkModeFullScreen();
        setStatusBarColor(Color.TRANSPARENT);
    }

    /**
     * 字图标 变黑 全屏没有占位
     */
    protected void setDarkModeFullScreen() {
        if (null == getWindow()) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            setMIUIStatusBarDarkIcon(true);
            setMeizuStatusBarDarkIcon(true);
            if (null != getWindow().getDecorView()) {
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            }
        }
    }

    /**
     * 字图标 变白 全屏没有占位
     */
    public void setLightModeFullScreen() {
        if (null == getWindow()) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            setMIUIStatusBarDarkIcon(false);
            setMeizuStatusBarDarkIcon(false);
            if (null != getWindow().getDecorView()) {
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            }
        }
    }

    protected void setStatusBarColor(int color) {
        if (null == getWindow()) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            getWindow().setStatusBarColor(color);
        } else {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    /**
     * 修改 MIUI V6  以上状态栏颜色
     */
    private void setMIUIStatusBarDarkIcon(boolean darkIcon) {
        if (null == getWindow()) {
            return;
        }
        Class<? extends Window> clazz = getWindow().getClass();
        try {
            Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
            Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
            int darkModeFlag = field.getInt(layoutParams);
            Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
            extraFlagField.invoke(getWindow(), darkIcon ? darkModeFlag : 0, darkModeFlag);
        } catch (Exception e) {
            //e.printStackTrace();
        }
    }

    /**
     * 修改魅族状态栏字体颜色 Flyme 4.0
     */
    private void setMeizuStatusBarDarkIcon(boolean darkIcon) {
        if (null == getWindow()) {
            return;
        }
        try {
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            Field darkFlag = WindowManager.LayoutParams.class.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
            Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags");
            darkFlag.setAccessible(true);
            meizuFlags.setAccessible(true);
            int bit = darkFlag.getInt(null);
            int value = meizuFlags.getInt(lp);
            if (darkIcon) {
                value |= bit;
            } else {
                value &= ~bit;
            }
            meizuFlags.setInt(lp, value);
            getWindow().setAttributes(lp);
        } catch (Exception e) {
            //e.printStackTrace();
        }
    }
}